<script type="text/javascript">
    function commit()
    {		
        var faction = document.getElementById("faction");
        var form = document.getElementById("fr");
		if(form.category_id.value == 0)
		{
			alert("Bạn cần chọn danh mục cho bài viết");
		}
		else
		{
			if (faction.value == 'add')
				form.action += 'add';
			else
				form.action += 'edit/' <?php if (isset($id)) echo "+ ".$id; else echo ""; ?>;
			form.submit();
		}
    }
</script>
<div class="art-post">
	<div class="art-post-tl"></div>
	<div class="art-post-tr"></div>
	<div class="art-post-bl"></div>
	<div class="art-post-br"></div>
	<div class="art-post-tc"></div>
	<div class="art-post-bc"></div>
	<div class="art-post-cl"></div>
	<div class="art-post-cr"></div>
	<div class="art-post-cc"></div>
	<div class="art-post-body">
	<div class="art-post-inner art-article">
        <h2 class="art-postheader">[<?php echo ($action=='add')?'Thêm':'Sửa'; ?> bài viết]</h2>
                    <form action="<?php echo Utility::getSiteUrl();?>/post/" method="post" id="fr">
        <span id="message" style="color:red;font-weight:bold;background-color:#f0f8ff;">
        <?php
         if(isset($error))
         {
			?>
			<div id="message-red">
				<table border="0" width="100%" cellpadding="0" cellspacing="0">
			<?php
            foreach($error as $err)
            {
				?>
					<tr>
						<td class="red-left"><?php echo $err; ?></td>
						<td class="red-right"><a class="close-red"><img src="<?php echo Utility::getSiteUrl();?>/public/images/table/icon_close_red.gif"   alt="" /></a></td>
					</tr>
				<?php
            }
			?>
			</table>
				</div>
				<?php
         }

        if(isset($message))
        {
			?>
			<div id="message-green">
				<table border="0" width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="green-left"><?php echo $message; ?></td>
					<td class="green-right"><a class="close-green"><img src="<?php echo Utility::getSiteUrl();?>/public/images/table/icon_close_green.gif"   alt="" /></a></td>
				</tr>
				</table>
			</div>
			<?php            
        }

        if(isset($inserted_id))
        {
            global $base_url;
            $link = $base_url . '/news/show/' . $inserted_id;
			?>
			<div id="message-blue">
				<table border="0" width="100%" cellpadding="0" cellspacing="0">
				<tr>
					<td class="blue-left"><?php echo " Xem bài viết tại : <a href='$link'>$link</a> <br/>";?></td>
					<td class="blue-right"><a class="close-blue"><img src="<?php echo Utility::getSiteUrl();?>/public/images/table/icon_close_blue.gif"   alt="" /></a></td>
				</tr>
				</table>
				</div>
			<?php
        }
        echo "<br/>";
        ?>
    </span>
        <input type="hidden" name="faction" id="faction" value="<?php echo $action; ?>" />
        <table border="0" cellpadding="0" cellspacing="0"  id="id-form">
            <tr>
                <th valign="top">Tiêu đề</th>
                    <td><input type="text" class="inp-form" name="title" value="<?php  if(isset($input)) if($action == 'edit') echo $input->title; else echo $input['title'];?>"/></td>
            </tr>
            <tr>
                <th valign="top">Tóm tắt</th>
                    <td><textarea  name="summary" class="form-textarea" style="width:380px;"><?php  if(isset($input)) if($action == 'edit') echo $input->summary; else echo $input['summary'];?></textarea></td>
                </td>
            </tr>
            <tr>
                <th valign="top">Nội dung</th>
				<td><textarea rows="5" cols="50" name="detail" id="detail" class="form-textarea"><?php  if(isset($input)) if($action == 'edit') echo $input->
                    detail; else echo $input['detail'];?></textarea>
                </td>
            </tr>
            <script type="text/javascript">
                CKEDITOR.replace('detail');
            </script>
            <tr>
                <th valign="top">Ảnh đại diện</th>
                    <td><input
                        type="button" value="thêm ảnh"
                        onclick="window.open('<?php echo Utility::getSiteUrl(); ?>/upload ','Upload','menubar=no,width=800,height=500,toolbar=no')"/>						
                    <br/><br/>
                    
                    <?php
					if(isset($input))
					{
						if($action == 'edit')
							$avatar =& $input->avatar;
						else
							$avatar =& $input['avatar'];
						if(strlen($avatar) > 0)
						{
							?>
							<img src="<?php echo $avatar;?>" style="max-width:200px"/>
							<br/>
							<?php
						}
					}
                      ?>
                    <input type="text" class="inp-form" name="avatar"  value="<?php  if(isset($input)) if($action == 'edit') echo $input->avatar; else echo $input['avatar']; ?>"/>
                </td>
            </tr>
            <tr>
                <th valign="top">
                    Danh mục</th>
                    <td><select name="category_id"  class="styledselect_form_1">
                        <option value="0">Chọn danh mục</option>
                        <?php
                        if(isset($cat_list))
                        {
                            if($action == 'edit')
                            {
                                foreach($cat_list as $cat)
                                {
                                    if(isset($id) && $id == $cat->id) continue;
                                    echo "<option value='$cat->id'";
                                    if(isset($input) && $input->category_id == $cat->id) echo "selected='selected'";
                                    echo ">";
                                    for($i = 0; $i < $cat->c_level; $i++) echo "--";
                                    echo " $cat->name</option>";
                                }
                            }
                            else
                            {
                                foreach($cat_list as $cat)
                                {
                                    if(isset($id) && $id == $cat->id) continue;
                                    echo "<option value='$cat->id'";
                                    if(isset($input) && $input['category_id'] == $cat->id) echo "selected='selected'";
                                    echo ">";
                                    for($i = 0; $i < $cat->c_level; $i++) echo "--";
                                    echo " $cat->name</option>";
                                }
                            }
                        }
                        ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th valign="top">
                    Trạng thái</th>
                    <td><select class="styledselect_form_1" name="status" style="width:305px;">
                        <option value="1" <?php if(isset($input)) if($action == 'edit') {if($input->status == 1) echo "selected='selected'";} else {if($input['status'] == 1)
                        echo "selected='selected'";} ?> >Hiển thị</option>
                        <option value="0"
                        <?php if(isset($input)) if($action == 'edit') {if($input->status == 0) echo
                        "selected='selected'";} else {if($input['status'] == 0)
                        echo "selected='selected'";} ?>>Không hiển thị</option>
                    </select>
                </td>
            </tr>
            <tr>
                <th valign="top">
                    Cho phép bình luận ?</th>
                    <td>
						<select name="comment_enable" style="width:305px;" class="styledselect_form_1">
							<option value="1"
							<?php if(isset($input)) if($action == 'edit') {if($input->comment_enable == 1) echo
							"selected='selected'";} else {if($input['comment_enable'] == 1)
							echo "selected='selected'";} ?> >Có</option>
							<option value="0"
							<?php if(isset($input)) if($action == 'edit') {if($input->comment_enable == 0) echo
							"selected='selected'";} else {if($input['comment_enable'] == 0)
							echo "selected='selected'";} ?>>Không</option>
						</select>
                </td>
            </tr>
            <tr>
				<td></td>
                <td><input type="button" value="<?php if($action =='add') echo "Thêm"; else echo "Sửa";?>" onclick="commit()">&nbsp;&nbsp;<input type="reset" value="Nhập lại"></td>
            </tr>
        </table>
    </form>

</div>
  
		<div class="cleared"></div>
	  </div>
  </div>